<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/static/css/regist.css">
<script type="text/javascript"
	src="${pageContext.request.contextPath }/static/js/jquery-2.1.3.min.js"></script>
<script type="text/javascript">
	function checkUsername() {
		var username = $("#username").val();
		if (username == "") {
			$("#usernameResult").text("请输入账号");
		} else {
			$
					.ajax({
						type : "POST",
						url : "${pageContext.request.contextPath}/registController/checkUsername.do",
						data : {
							"username" : username
						},
						async : true,
						cache : false,
						success : function(data) {
							if (data == "default") {
								$("#usernameResult").text("账号已存在");
							} else {
								$("#usernameResult").text("");
							}
						},
						error : function() {
							alert("系统错误");
						}
					});
		}
	}
	function checkPassword(){
		var password = $("#password").val();
		if(password == ""){
			$("#passwordResult").text("请输入密码");
		}else{
			$("#passwordResult").text("");
		}
	}
	function regist() {
		var username = $("#username").val();
		var password = $("#password").val();
		var age = $("#age").val();
		$
				.ajax({
					type : "POST",
					url : "${pageContext.request.contextPath}/registController/regist.do",
					data : {
						"username" : username,
						"password" : password,
						"age" : age
					},
					async : false,
					cache : false,
					success : function(data) {
						if (data == "succeed") {
							alert("注册成功");
							window.location.href = "${pageContext.request.contextPath}/welcomeController/welcome.do";
						} else if (data == "sameUsername") {
							alert("用户名已存在");
						} else {
							alert("注册失败");
						}
					},
					error : function() {
						alert("系统错误");
					}
				});
	}
	function back() {
		window.location.href = "${pageContext.request.contextPath}/welcomeController/welcome.do";
	}
</script>
<title>My JSP 'regist.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

</head>

<body>
	<div class="main">
		<div class="content">
			<span>账号：</span>
			<input id="username" class="required" onblur="checkUsername()" />
			<span id="usernameResult"></span>
			<br /> 
			<span>密码：</span>
			<input id="password" class="required" onblur="checkPassword()" type="password" />
			<span id="passwordResult"></span>
			<br />
			<span>年龄：</span>
			<input id="age" name="age" />
		</div>
		<div class="choice">
			<button id="regist_btn" onclick="regist()">注册</button>
			<button id="back_btn" onclick="back()">返回</button>
		</div>
	</div>
</body>
</html>
